package com.pb.wkflow.api.controller.assembler.mapper;

import com.pb.infra.oth.util.UserInfo;
import com.pb.wkflow.api.controller.request.ProcDefineCreateRequest;
import com.pb.wkflow.api.controller.request.ProcDefineJsonRequest;
import com.pb.wkflow.api.entity.WfSysJsonXmlEntity;
import com.pb.wkflow.core.utils.DateUtils;
import com.pb.wkflow.core.utils.SnowFlake;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

import java.util.Date;

/**
 * WfSysJsonXmlDtoMapper
 * @author 苗益辉
 * @date 2022/12/27 10:27
 */
@Mapper(imports = {SnowFlake.class, DateUtils.class, Date.class})
public interface WfSysJsonXmlDtoMapper {

    WfSysJsonXmlDtoMapper INSTANCE = Mappers.getMapper(WfSysJsonXmlDtoMapper.class);

    /**
     * assemblerToQuery
     * @param request request
     * @param userInfo userInfo
     * @return WfSysJsonXmlEntity
     */
    @Mapping(target = "procDefKey", source = "request.procDefineKey")
    @Mapping(target = "tenantId", source = "userInfo.corpId")
    @Mapping(target = "ver", source = "request.version")
    WfSysJsonXmlEntity assemblerToQuery(ProcDefineJsonRequest request, UserInfo userInfo);


    /**
     * assemblerToQuery
     * @param request request
     * @param userInfo userInfo
     * @return
     */
    @Mapping(target = "procDefKey", source = "request.key")
    @Mapping(target = "tenantId", source = "userInfo.corpId")
    @Mapping(target = "btnType", source = "request.wkflowBtnType")
    WfSysJsonXmlEntity assemblerToQuery(ProcDefineCreateRequest request, UserInfo userInfo);

    /**
     * assemblerToSave
     * @param request request
     * @param userInfo userInfo
     * @return WfSysJsonXmlEntity
     */
    @Mapping(target = "id", expression = "java(SnowFlake.nextId())")
    @Mapping(target = "procDefKey", source = "request.key")
    @Mapping(target = "jsonXml", source = "request.jsonXml")
    @Mapping(target = "isDeploy", expression = "java(0)")
    @Mapping(target = "tenantId", source = "userInfo.corpId")
    @Mapping(target = "description", source = "request.description")
    @Mapping(target = "createTime", expression = "java(DateUtils.dateToStr(new Date()))")
    @Mapping(target = "updateTime", expression = "java(DateUtils.dateToStr(new Date()))")
    @Mapping(target = "btnType", source = "request.wkflowBtnType")
    WfSysJsonXmlEntity assemblerToSave(ProcDefineCreateRequest request, UserInfo userInfo);

    /**
     * assemblerToUpdate
     * @param request request
     * @param entity entity
     */
    @Mapping(target = "jsonXml", source = "jsonXml")
    @Mapping(target = "description", source = "description")
    @Mapping(target = "updateTime", expression = "java(DateUtils.dateToStr(new Date()))")
    @Mapping(target = "btnType", source = "wkflowBtnType")
    @Mapping(target = "id", ignore = true)
    void assemblerToUpdate(ProcDefineCreateRequest request,@MappingTarget WfSysJsonXmlEntity entity);

    /**
     * assemblerToDeploy
     * @param processDefinition processDefinition
     * @param entity entity
     */
    @Mapping(target = "isDeploy", expression = "java(1)")
    @Mapping(target = "updateTime", expression = "java(DateUtils.dateToStr(new Date()))")
    @Mapping(target = "procDefId", source = "processDefinition.id")
    @Mapping(target = "id", ignore = true)
    void assemblerToDeploy(ProcessDefinition processDefinition,@MappingTarget  WfSysJsonXmlEntity entity);
}
